package model

import (
	"video-management-system/userrpc/userrpc"

	"gorm.io/gorm"
)

type CommentInfo struct {
	gorm.Model
	Vid            int    `gorm:"type:int"`
	Uid            int    `gorm:"type:int"`
	CommentContent string `gorm:"type:varchar(500);not null"`
	IsAnonymous    string `gorm:"type:enum('0','1');default:'0';not null"`
	CommentStatus  string `gorm:"type:enum('0','1');default:'0';not null"`
	Rid            int    `gorm:"type:int"`
}

func (i *CommentInfo) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(i).Error
}

func (i *CommentInfo) FindCommentById(db *gorm.DB, in *userrpc.CommentDelRequest) error {
	return db.Debug().Where("uid=?", in.Uid).Where("vid=?", in.Vid).Find(i).Error
}

func (i *CommentInfo) CommentDel(db *gorm.DB, in *userrpc.CommentDelRequest) error {
	return db.Debug().Where("uid=?", in.Uid).Where("vid=?", in.Vid).Delete(i).Error
}

func (i *CommentInfo) CollectDel(db *gorm.DB, in *userrpc.CollectDelRequest) error {
	return db.Debug().Where("uid=?", in.Uid).Where("vid=?", in.Vid).Delete(i).Error
}

func CommentList(db *gorm.DB, vid int64) []*userrpc.CommentList {
	var list []*userrpc.CommentList
	db.Debug().Model(CommentInfo{}).Select("users.*,comment_infos.*").
		Joins("left join users on comment_infos.uid = users.id").Find(&list)
	return list
}

func CollectList(db *gorm.DB, in *userrpc.CollectListRequest) []*userrpc.CollectList {
	var list []*userrpc.CollectList
	db.Debug().Model(&CollectionInfo{}).Select("users.nickname as nickname,video_infos.*").
		Joins("LEFT JOIN users on users.id= collection_infos.uid").
		Joins("LEFT JOIN video_infos on video_infos.id= collection_infos.vid").
		Where("collection_infos.uid = ?", in.Uid).
		Scopes(Paginate(int(in.Page), int(in.Size))).Order("collection_infos.created_at desc").
		Find(&list)
	return list
}
